<template>
  <div class="img-div">
    <div v-if="showImgURL && simpleShow" :style="getWrapStyle">
      <Image :width="size" :style="{ height: size+'px' }" :src="showImgURL" :preview="{ visible: false }" @click.stop="visible = true" />
      <div style="display: none">
        <PreviewGroup :preview="{ visible, onVisibleChange: (vis: boolean) => (visible = vis), current: curShowIdx }">
          <template v-for="(img, index) in imgList" :key="img+index">
            <Image :width="size" :src="img" :index="index" />
          </template>
        </PreviewGroup>
      </div>
    </div>
    <PreviewGroup v-else>
      <template v-for="(img, index) in imgList" :key="img">
        <Image
          :width="size"
          :style="{ marginLeft: index === 0 ? 0 : margin }"
          :src="img"
        />
      </template>
    </PreviewGroup>
  </div>
</template>
<script lang="ts">
  import type { CSSProperties } from 'vue';
  import { defineComponent, computed } from 'vue';
  import { Image, Badge } from 'ant-design-vue';
  import { propTypes } from '@/utils/propTypes';

  export default defineComponent({
    name: 'TableImage',
    components: { Image, PreviewGroup: Image.PreviewGroup, Badge },
    props: {
      imgList: propTypes.arrayOf(propTypes.string),
      size: propTypes.number.def(40),
      // 是否简单显示（只显示第一张图片）
      simpleShow: propTypes.bool,
      // 简单模式下是否显示图片数量的badge
      showBadge: propTypes.bool.def(true),
      // 图片间距
      margin: propTypes.number.def(4),
      // src前缀，将会附加在imgList中每一项之前
      // srcPrefix: propTypes.string.def(''),
      // 当前需要展示的图片下标
      curShowIdx: propTypes.number.def(0),
    },
    setup(props) {
      const getWrapStyle = computed((): CSSProperties => {
        const { size } = props;
        const s = `${size}px`;
        return { height: s, width: s };
      });

      const showImgURL = computed(() => {
        const { imgList }:any = props;
        return imgList[0] || '';
      });

      const visible = ref(false);

      return { getWrapStyle, showImgURL, visible };
    },
  });
</script>
<style lang="less" scoped>
</style>
